package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn1,btn2;
    TextView tv1,tv2;
    EditText ed1,ed2;
    //String name,pwd,img_avatar;
    String name,pwd;
    String value1,value2,value3;
    //头像传值
    //ImageView img_avatar;
    //private ImageView img;
    ImageView img;
    int k;
    //int[] imgs;
    int[] imgs = new int[]{
            R.drawable.touxiang1,
            R.drawable.touxiang2
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnLogin();
        btnEnroll();
    }
    private void initView() {
        btn1=findViewById(R.id.button);
        btn2=findViewById(R.id.button2);
        tv1=findViewById(R.id.textView_name);
        tv2=findViewById(R.id.textView2);
        ed1=findViewById(R.id.editText_PersonName);
        ed2=findViewById(R.id.editText_Password);
        img=findViewById(R.id.imageView_avatar);
    }
    private void btnLogin() {
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();//管理员跳转
                Intent intent1 = new Intent();//注册页面
                Intent intent2 = new Intent();//注册信息的显示跳转的

                name = ed1.getText().toString().trim();
                pwd = ed2.getText().toString().trim();
                //img = img.getImageAlpha().to().trim();

                 intent1 = getIntent();//用于当注册页面成功后，返回主界面时候获取用户名字和密码的

                value1 = intent1.getStringExtra("name");
                value2 = intent1.getStringExtra("pwd");
                value3 = intent1.getStringExtra("school");
                k = intent1.getIntExtra("image", 0);
                img.setBackgroundResource(imgs[k]);

                if (name.equals("admin") && pwd.equals("admin")) {//管理员密码
                    intent.setClass(MainActivity.this, RootActivity.class);
                    startActivity(intent);
                }
                else if(name.equals(value1) && pwd.equals(value2))
                {
                    intent2.setClass(MainActivity.this,MainActivity3.class);
                    intent2.putExtra("name",value1);
                    intent2.putExtra("pwd",value2);
                    intent2.putExtra("school",value3);
                    intent2.putExtra("image",k);
                    startActivity(intent2);
                }
                else
                {
                    Toast.makeText(getApplicationContext(),"用户名/密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    private void btnEnroll() {
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3 = new Intent();
                intent3.setClass(MainActivity.this,MainActivity13.class);
                startActivity(intent3);
            }
        });

    }

}